/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;


import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.StructuralFeature;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.core.ui.dialogs.SlotDefiningStructuralFeatureTreeSelectionDialog;

/**
 * 
 */
public class SlotStructuralFeatureSection extends PropertyViewSection {


	/**
	 * 
	 */
	private Button browseButton, removeButton; 
	
	/**
	 * 
	 */
	private CLabel SFLabel;
	
	/**
	 * 
	 */
	private Composite parent;
	
	/**
	 * Listener for the delete button.
	 */
	protected MouseListener removeButtonListener;
	
	/**
	 * Listener for the up button.
	 */
	protected MouseListener browseButtonListener;

	
	/**
	 * 
	 */
	private final org.eclipse.swt.graphics.Image remove_img 
	= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"Delete_16x16.gif");
	
	/**
	 * 
	 */
	private final org.eclipse.swt.graphics.Image add_img
	= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"Add_16x16.gif");


	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		this.parent=parent;
		FormData data;


		
		getWidgetFactory().createCLabel(composite, "Defining Feature:"); //$NON-NLS-1$
		
				
		// defining structural feature label
		SFLabel = getWidgetFactory().createCLabel(composite, "", SWT.BORDER);
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH+STANDARD_LABEL_WIDTH/2);
		data.right = new FormAttachment(30);
		data.top = new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		SFLabel.setLayoutData(data);
		
		// browseButton layout
		browseButton = getWidgetFactory().createButton(composite, "", SWT.PUSH);
		browseButton.setImage(add_img);
		data = new FormData();
		data.left = new FormAttachment(SFLabel, ITabbedPropertyConstants.HSPACE);
		data.top  = new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		browseButton.setLayoutData(data);

		// removeButton layout
		removeButton = getWidgetFactory().createButton(composite, "", SWT.PUSH);
		removeButton.setImage(remove_img);
		data = new FormData();
		data.left = new FormAttachment(browseButton, ITabbedPropertyConstants.HSPACE);
		data.top  = new FormAttachment(browseButton, 0, SWT.CENTER);
		removeButton.setLayoutData(data);
		
		// listeners setting
		browseButtonListener = new BrowseButtonlistener();
		browseButton.addMouseListener(browseButtonListener);
		removeButtonListener = new RemoveButtonlistener();
		removeButton.addMouseListener(removeButtonListener);
	}



	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		if(!browseButton.isDisposed()){
			if(getElement() instanceof Slot){
				if(((Slot)getElement()).getDefiningFeature()==null) {
					SFLabel.setText("UndefinedSlot");
					removeButton.setEnabled(false);
				}

				else {
					SFLabel.setText(((Slot)getElement()).getDefiningFeature().getQualifiedName());
					removeButton.setEnabled(true);
				}
			}
		}
	}
	
	
	/**
	 * Listener for the Remove Button
	 * Specific behavior is implemented in removeButtonPressed.
	 * 
	 * @author Chokri Mraidha
	 */
	protected class RemoveButtonlistener implements MouseListener {
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void mouseDoubleClick(MouseEvent e) {
			// do nothing
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void mouseDown(MouseEvent e) {
			// do nothing
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void mouseUp(MouseEvent e) {
			removeButtonPressed();
			refresh();
		}
	}
	
	
	/**
	 * Listener for the Remove Button
	 * Specific behavior is implemented in browseButtonPressed.
	 * 
	 * @author Chokri Mraidha
	 */
	protected class BrowseButtonlistener implements MouseListener {
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void mouseDoubleClick(MouseEvent e) {
			// do nothing
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void mouseDown(MouseEvent e) {
			// do nothing
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void mouseUp(MouseEvent e) {
			browseButtonPressed();
			refresh();
		}
	}

	/**
	 * behavior executed when remove button is pressed.
	 */
	protected void removeButtonPressed(){
		if(getElement() instanceof Slot){
			((Slot)getElement()).setDefiningFeature(null);
		}
	}
	
	/**
	 * behavior executed when browse button is pressed.
	 */
	protected void browseButtonPressed(){
		if(getElement() instanceof Slot){
			SlotDefiningStructuralFeatureTreeSelectionDialog dialog = new SlotDefiningStructuralFeatureTreeSelectionDialog(parent.getShell(), (Slot)getElement());
			int result = dialog.open();
			
			if(result==IDialogConstants.OK_ID){
				StructuralFeature definingFeature = dialog.getResult(); 
				if(definingFeature != null){
					((Slot)getElement()).setDefiningFeature(definingFeature);
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(removeButton!=null && !removeButton.isDisposed())
			removeButton.removeMouseListener(removeButtonListener);
		if(browseButton!=null && !browseButton.isDisposed())
			browseButton.removeMouseListener(browseButtonListener);
	}
	
}


